package com.example.hotel_manage.service.position;

import com.example.common.constant.ErrorCode;
import com.example.common.exception.BusinessException;
import com.example.common.pojo.qo.OptionQO;
import com.example.common.pojo.vo.OptionVO;
import com.example.common.pojo.vo.PageVO;
import com.example.hotel_manage.dao.employee.EmployeeDAO;
import com.example.hotel_manage.dao.position.PositionDAO;
import com.example.hotel_manage.pojo.dto.position.PositionAddDTO;
import com.example.hotel_manage.pojo.dto.position.PositionUpdateDTO;
import com.example.hotel_manage.pojo.mapper.position.PositionMapper;
import com.example.hotel_manage.pojo.po.position.PositionPO;
import com.example.hotel_manage.pojo.qo.position.PositionQO;
import com.example.hotel_manage.pojo.vo.position.PositionListVO;
import com.example.hotel_manage.pojo.vo.position.PositionShowVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 【职位】增删改查
 */
@Service
public class PositionService {

    @Autowired
    private EmployeeDAO employeeDAO;
    @Autowired
    private PositionDAO positionDAO;


    /**
     * 新增【职位】
     *
     * @param positionDTO
     * @return
     */
    @Transactional(rollbackFor = RuntimeException.class)
    public PositionPO save(PositionAddDTO positionDTO) {
        PositionPO position = PositionMapper.INSTANCE.fromAddDTO(positionDTO);
        positionDAO.save(position);
        return position;
    }

    /**
     * 修改【职位】
     *
     * @param positionUpdateDTO
     * @return
     */
    @Transactional(rollbackFor = RuntimeException.class)
    public PositionPO update(PositionUpdateDTO positionUpdateDTO) {
        Integer id = positionUpdateDTO.getId();
        PositionPO position = this.getPosition(id, true);
        PositionMapper.INSTANCE.setUpdateDTO(position, positionUpdateDTO);
        positionDAO.update(position);
        return position;
    }

    /**
     * 查询分页列表
     *
     * @param positionQO
     * @return
     */
    public PageVO<PositionListVO> list(PositionQO positionQO) {
        PageVO<PositionListVO> page = positionDAO.findByPage(positionQO);
        return page;
    }

    /**
     * 查询【职位】选项列表
     *
     * @return
     */
    public List<OptionVO<Integer, String>> findOptions(OptionQO<Integer, String> qo) {
        List<OptionVO<Integer, String>> options = positionDAO.findOptions(qo);
        return options;
    }

    /**
     * 根据主键获取【职位】
     *
     * @param id    主键
     * @param force 是否强制获取
     * @return
     */
    public PositionPO getPosition(Integer id, boolean force) {
        PositionPO position = positionDAO.findById(id);
        if (force && position == null) {
            throw new BusinessException(ErrorCode.RECORD_NOT_FIND);
        }
        return position;
    }


    /**
     * 查询【职位】详情
     *
     * @param id
     * @return
     */
    public PositionShowVO show(Integer id) {
        PositionPO position = this.getPosition(id, true);
        PositionShowVO showVO = PositionMapper.INSTANCE.toShowVO(position);
        return showVO;
    }

    /**
     * 删除【职位】
     *
     * @param ids
     * @return
     */
    @Transactional(rollbackFor = RuntimeException.class)
    public int delete(Integer... ids) {
        int count = 0;
        for (Integer id : ids) {
            this.checkDeleteByEmployee(id);
            count += positionDAO.delete(id);
        }
        return count;
    }

    /**
     * 校验是否存在【员工】关联
     *
     * @param id
     */
    private void checkDeleteByEmployee(Integer id) {
        int count = employeeDAO.getCountByP_id(id);
        if (count > 0) {
            throw new BusinessException(ErrorCode.CASCADE_DELETE_ERROR);
        }
    }


}


